package com.museum.utils;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.AllClientPNames;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;

public class Utils {
	private static final Log log = LogFactory.getLog(Utils.class);

	public static final String agent = "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2";
	public static final String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
	public static final HttpPost DEFAULT_POST = createPost();

	public static HttpPost createPost() {
		HttpPost post = new HttpPost();
		BasicHttpParams hp = new BasicHttpParams();
		hp.setParameter(AllClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		hp.setParameter(AllClientPNames.HANDLE_REDIRECTS, true);
		hp.setParameter(AllClientPNames.MAX_REDIRECTS, 5);
		hp.setParameter(AllClientPNames.CONNECTION_TIMEOUT, 2000);
		hp.setParameter(AllClientPNames.USE_EXPECT_CONTINUE, false);
		post.setParams(hp);
		post.setHeader("Host", "my.mowhere.com");
		post.setHeader("User-Agent", agent);
		post.setHeader("Accept", accept);
		post.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
		post.setHeader("Accept-Encoding", "gzip, deflate");
		post.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
		post.setHeader("Connection", "keep-alive");
		post.setHeader("Referer", "http://my.mowhere.com/index.php?my_sig_uId=4209691217&my_sig_sId=1716791&my_sig_time=1328857705&my_sig_sessionId=4f34c26903teliihf07fffd10552b3ac&my_sig_added=1&my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F&my_sig_appId=1044822&my_sig_page_app=userapp.php%3Fid%3D1044822%26my_suffix%3D&my_sig_page_login=do.php%3Fac%3Dlogin%26refer%3Duserapp.php%253Fid%253D1044822&my_sig_page_install=cp.php%3Fac%3Duserapp%26appid%3D1044822&my_sig_in_iframe=1&my_sig_v=0.1&my_sig_uLevel=USER&my_sig_key=e527d4b1462fcb60a74be6c23ceb0d39");
		return post;
	}

	public static HttpPost createPost(String cookie) {
		HttpPost post = createPost();
		post.setHeader("Cookie", cookie);
		return post;
	}

	public static HttpPost createPost(String url, String cookie) {
		HttpPost post = createPost();
		try {
			post.setURI(new URI(url));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		post.setHeader("Cookie", cookie);
		return post;
	}

	public static String trans(HttpResponse response) throws Exception {
		HttpEntity entity = response.getEntity();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		entity.writeTo(baos);
		String responseJson = baos.toString("UTF-8");
		log.trace("response json : " + responseJson);
		return responseJson;
	}

	public static int getRandomInt(int min, int max) {
		Random r = new Random();
		return r.nextInt(max - min + 1) + min;
	}

	public static int getRandomInt(int max) {
		Random r = new Random();
		return r.nextInt(max + 1);
	}

	public static boolean isBlank(String s) {
		return s == null || "".equals(s.trim());
	}

	public static HttpEntity genWaEntity(String uid, String x, String y)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("x", x));
		nvps.add(new BasicNameValuePair("y", y));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genFriendEntity(String uid, String x, String y)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genBuyEntity(String uid, String item)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "buy"));
		nvps.add(new BasicNameValuePair("item_id", item));
		nvps.add(new BasicNameValuePair("quantity", "1"));
		nvps.add(new BasicNameValuePair("type", "wealth"));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genUseEntity(String uid, String item)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "eat"));
		nvps.add(new BasicNameValuePair("item_id", item));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genSellEntity(String uid, String flag)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "sellbatch"));
		nvps.add(new BasicNameValuePair("flag", flag));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genUseritemEntity(String uid) throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("type", "5"));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genHarvEntity(String uid, String item_id)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "get"));
		nvps.add(new BasicNameValuePair("item_id", item_id));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genMakeEntity(String uid, String item_id)
			throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "make"));
		nvps.add(new BasicNameValuePair("flag", "true"));
		nvps.add(new BasicNameValuePair("item_id", item_id));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genDigBatchEntity(String uid) throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "digbatch"));
		nvps.add(new BasicNameValuePair("flag", "true"));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}

	public static HttpEntity genDigBatchOkEntity(String uid) throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("myuid", uid));
		nvps.add(new BasicNameValuePair("rnd", "1316836127264"));
		nvps.add(new BasicNameValuePair("action", "digbatchok"));
		HttpEntity he = new UrlEncodedFormEntity(nvps, "UTF-8");
		return he;
	}
}
